package com.retail.order.controller;

import com.retail.core.common.AbstractCommonController;
import com.retail.core.entity.ParamReq;

import com.retail.core.entity.result.PageResult;
import com.retail.core.entity.result.Result;
import com.retail.global.security.LoginUser;
import com.retail.global.security.LoginUserUtil;
import com.retail.global.annon.TraceLog;
import com.retail.order.client.RetailSettleOrderClient;
import com.retail.order.entity.request.RetailSettleOrderRequest;
import com.retail.order.entity.vo.RetailSettleOrderVo;
import com.retail.order.service.RetailSettleOrderService;
import com.retail.system.entity.vo.TableTreeVo;
import com.retail.system.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单信息 API
 *
 * @author Administrator
 */
@Api(tags = "订单信息 API")
@RestController
@RequestMapping("/retailOrder")
public class RetailSettleOrderController extends AbstractCommonController<RetailSettleOrderService, RetailSettleOrderRequest, RetailSettleOrderVo> {

    @Autowired
    private RetailSettleOrderService retailSettleOrderService;

    @Autowired
    private RetailSettleOrderClient retailSettleOrderClient;

    @Autowired
    private AdminService adminService;

    @TraceLog()
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(retailSettleOrderService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存订单信息")
    public Result<Integer> save(@RequestBody RetailSettleOrderRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.save(retailSettleOrderService, request);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新订单信息")
    public Result<Integer> update(@RequestBody RetailSettleOrderRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.update(retailSettleOrderService, request);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("订单信息-查询")
    public Result<List<RetailSettleOrderVo>> listPage(@RequestBody ParamReq<RetailSettleOrderRequest> req) {
        return super.listPage(retailSettleOrderService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("订单信息-统计")
    public Result<Integer> countPage(@RequestBody ParamReq<RetailSettleOrderRequest> req) {
        return super.countPage(retailSettleOrderService, req);
    }

    @TraceLog()
    @PostMapping("multiFile")
    @ApiOperation("订单信息-统计")
    public Result<Integer> multiFile(Long fileId) {
        return retailSettleOrderClient.multiFile(fileId);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("订单信息-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<RetailSettleOrderRequest> req) {
        RetailSettleOrderRequest adminRequest = req.getEntity();
        Long adminId = getLoginUserId();
        LoginUser loginUser = LoginUserUtil.getPrincipal();
        adminRequest.setUserType(loginUser.getUserType());
        adminRequest.setSearchOwnerUserId(adminId);
        req.setEntity(adminRequest);

        return super.page(retailSettleOrderService, req);
    }
    

    /**
     * 我的商户
     * @param adminId
     * @return
     */
    @GetMapping("adminOrderTableTree/{adminId}")
    public Result<List<TableTreeVo>> adminOrderTableTree(@PathVariable("adminId") Long adminId) {
        return retailSettleOrderClient.adminOrderTableTree(adminId);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除订单信息")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(retailSettleOrderService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除订单信息")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(@PathVariable("ids") List<Long> ids) {
        return super.batchDelete(retailSettleOrderService, ids);
    }


    /**
     * 订单统计 (代操作)
     * @param req
     * @return
     */
    @TraceLog()
    @PostMapping("orderPageStastics")
    @ApiOperation("订单信息-分页数据")
    public Result<PageResult> orderPageStastics(@RequestBody ParamReq<RetailSettleOrderRequest> req) {
        RetailSettleOrderRequest adminRequest = req.getEntity();
        Long adminId = getLoginUserId();
        LoginUser loginUser = LoginUserUtil.getPrincipal();
        adminRequest.setUserType(loginUser.getUserType());
        adminRequest.setSearchOwnerUserId(adminId);
        req.setEntity(adminRequest);

        return super.page(retailSettleOrderService, req);
    }
    
    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }
}
